package org.xbzheng.restful_study.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xbzheng.restful_study.dao.RoomDao;
import org.xbzheng.restful_study.model.Room;
import org.xbzheng.restful_study.model.RoomColumn;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @Created by szheng on 2016/11/1.
 */
@Service
public class RoomService {

    @Autowired
    private RoomDao roomDao;

    public List<RoomColumn> createRoom(final List<RoomColumn> roomColumns){
        if(roomColumns.isEmpty()){
            return null;
        }
        List<RoomColumn> results = new ArrayList<>();
        Room room = roomColumns.get(0).getRoom();
        room.setStartTime(new Date());
        room = roomDao.save(room);

        for(int i = 0; i < roomColumns.size(); i++){
            final RoomColumn roomColumn = roomColumns.get(i);
            roomColumn.setRoom(room);
            results.add(roomDao.save(roomColumn));
        }
        return results;
    }

    public List<Room> findRooms(){
        return roomDao.findRooms();
    }

    public Room getRoom(final int roomId){
        final Room room = new Room();
        room.setId(roomId);

        return roomDao.findById(roomId);
    }

}
